package lelouet.datacenter.simulation.events;

import java.util.List;

import lelouet.datacenter.simulation.Event;
import lelouet.datacenter.simulation.EventHandler;
import lelouet.datacenter.simulation.VM;
import lelouet.datacenter.simulation.vms.AVM;

/** the VM has been switched of state */
public class VMStateChanged extends VMEvent {

	/** the different execution states of a {@link AVM} */
	public static enum VMStates {
		/** the VM is not added to the datacenter */
		EXTERNAL,
		/**
		 * the VM is waiting for the datacenter to put it on a hypervisor ; it
		 * cannot be started before being suspended to ram on an hypervisor
		 */
		UNALLOCATED,
		/** The VM is working on an hypervisor */
		RUNNING,
		/**
		 * the VM is sleeping on an hypervisor : it does not consume resources
		 * because
		 */
		SUSPENDED,
		/**
		 * the VM has no more work to do and can simply be removed from the
		 * hypervisor
		 */
		STOPPED,
		/** the VM has had a critical problem that made it crash */
		CRASHED
	}

	/**
	 * @param from
	 *            the {@link VMStates} the vm was in before
	 * @param to
	 *            the {@link VMStates} the vm has arrived to
	 */
	public VMStateChanged(VM vm, long time, VMStates from, VMStates to) {
		super(vm, time);
		before = from;
		after = to;
	}

	public final VMStates before;
	public final VMStates after;

	@Override
	public List<Event> apply(EventHandler handler) {
		return handler.handleStateChanged(this);
	}

	@Override
	public String toString() {
		return "VM <" + vm.getId() + "> changed state from <" + before.name()
				+ "> to <" + after.name() + ">";
	}

}
